package com.jl.card.parse.part;

import com.jl.card.parse.IAPDUComponent;
import com.jl.card.parse.part.simpleTLV.SimpleTLVTool;
import com.jl.card.parse.part.simpleTLV.SimpleTagConstants;
import com.jl.card.util.SimpleTLV;
import com.jl.card.util.SimpleTag;
import com.jl.util.HexUtil;

public class SimpleTLVPart extends APDUPart{
	
	private SimpleTLV tlv;
	private SimpleTag tag;
	private String res;
	
	public SimpleTLVPart(IAPDUComponent parent ,byte[] part){
		super(parent, part);
		this.tlv = new SimpleTLV(null, part.clone()).iterator().next();
		tag = this.tlv.getTag();
		analyse();
	}
	
	@Override
	public String getSummary(){
		StringBuilder sb = new StringBuilder();
		String data = HexUtil.toHexCompact(part);
		if(data.length() > 50){
			data = data.substring(0,20) + "..." + data.substring(data.length() - 20);
		}
		sb.append(data);
		sb.append("[");
		sb.append(res).append("]");
		return sb.toString();
	}

	@Override
	public void analyse() {
		StringBuilder sb = new StringBuilder();
		sb.append(SimpleTagConstants.getTagName(tag)).append('-');
		sb.append(SimpleTLVTool.getValueInfo(tlv));
		this.res = sb.toString();
	}

	@Override
	public void add(IAPDUComponent apduPart) {
	}
	
}
